<template>
  <div v-if="show" class="fontDa">
    <div class="customTableHeader">
      <div class="customTableTitle" style="font-size: 36px;">{{ cepingHdglb.hdName }}</div>
      <div style="height: 30px;" v-if="!djxxDjmx"></div>
      <div style="text-align: center; margin:10px 0; font-size:22px;" v-if="djxxDjmx">★请对被测评对象的总体情况综合评价</div>
    </div>
    <div v-for="(cp,xx) in cppList" :key="xx+10000" style="margin-bottom: 50px;">
      <div class="biaoTiGi">{{cp.cppName}}</div>
      <el-table
        :data="getXpList(cp)"
        border
        stripe
        style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            header-align="center"
            align="center"
            width="70">
          </el-table-column>
          <el-table-column
            label="测评对象"
            header-align="center">
            <template slot-scope="scope">
              <div class="xinYang">
                <div style="min-width:500px;">
                  <span v-if="scope.row.bcpDx=='部门'" class="deptName">
                    <i class="iconfont icon-untitled85"></i>
                    {{ scope.row.bcpDeptName }}领导班子
                  </span>
                  <span v-else>
                    <i class="el-icon-s-custom"></i>
                    {{ scope.row.bcpUserName }} {{ scope.row.userInfo ? scope.row.userInfo.ryZw : '' }}
                    <!--                      {{ scope.row.userInfo ? scope.row.userInfo.ryZjmc : '' }}-->
                  </span>
                </div>
                <div class="hzBox">
                  <djDf :xpId="scope.row.id"></djDf>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="分值"
            header-align="center"
            align="center"
            width="180">
            <template slot-scope="scope">
              <span class="red">
                {{ scope.row.cphzfz }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            header-align="center"
            align="center"
            width="180">
            <template slot-scope="scope">
              <el-tooltip content="点击可返回该张测评票进行测评分值修改。" placement="top" effect="light">
                <el-button type="warning" style="margin-left:10px" @click="open(scope.row)">返回测评界面</el-button>
              </el-tooltip>
            </template>
          </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {wdXuanPiao, wdXuanPiaoXp} from "@/api/ceping/cepinghdglxpxxb";
import {anonymousSelhdidList as selhdidList} from '@/api/ceping/cepinghdglcppmxb'
import {getObj} from "@/api/ceping/cepinghdglb";
import {getIds as deptsGet} from "@/api/admin/dept";
import {getIds as usersGet} from "@/api/admin/user";

import djDf from "./cpIndexHzDjdf";

export default {
  name: "cpIndexHz",
  components: {
    djDf
  },
  data() {
    return {
      show: true,
      type: "",
      hdId: null,
      xcCode: "",
      xpList: [],//所有选票
      cppList: [],
      cepingHdglb: {},
      deptObj: {},
      userObj: {},
      djxxDjmx :false,
    }
  },
  methods: {
    init() {
      this.xpList = [];
    },
    //初始按会议
    initHd(hdId) {
      this.init();
      //实名
      this.type = "HD";
      this.hdId = hdId;
      wdXuanPiao(hdId).then(res => {
        if (res.data.data.length > 0) {
          let data = res.data.data;
          this.initJG(data);
        } else {
          this.$notify.closeAll();
          this.$notify.error('会议中没有选票信息')
        }
      })
    },
    //初始按选票
    initXp(xcCode) {
      this.init();
      //匿名
      this.type = "XP";
      this.xcCode = xcCode;
      wdXuanPiaoXp(xcCode).then(res => {
        if (res.data.data.length > 0) {
          let data = res.data.data;
          this.initJG(data);
        } else {
          this.$notify.closeAll();
          this.$notify.error('票号中没有选票信息');
        }
      })
    },
    initJG(d) {
      let data = d.sort(compareZb);
      function compareZb(a, b) {
        return b.cphzfz - a.cphzfz;
      }
      this.cppList = [];
      let cppListId = [];

      selhdidList(data[0].hdId).then(res => {
        let list = res.data.data;
        this.djxxDjmx = false;
        for (var j in list) {
          if(!(list[j].djxxDjmx==""||list[j].djxxDjmx==null)){
            this.djxxDjmx = true;
          }
          if (cppListId.indexOf(list[j].cppId) == -1) {
            for (let i in data) {
              if (data[i].cppId == list[j].cppId) {
                this.cppList.push(list[j])
                cppListId.push(list[j].cppId)
                break;
              }
            }
          }
        }
        for (let i in data) {
          if (data[i].bcpDx == "部门") {
            this.xpList.push(data[i])
          }
        }
        for (let j in data) {
          if (data[j].bcpDx == "个人") {
            this.xpList.push(data[j])
          }
        }
       //console.log(this.cppList)
        this.getHdInfo();
        this.getDeptUser();
      })
    },
    getXpList(cp){
      let list = [];
      for(var i in this.xpList){
        if(this.xpList[i].cppId==cp.cppId){
          list.push(this.xpList[i])
        }
      }
      return list;
    },
    getHdInfo() {
      if (this.xpList.length > 0) {
        getObj(this.xpList[0].hdId).then(res => {
          this.cepingHdglb = res.data.data;
        })
      }
    },
    getDeptUser() {
      let deptIds = [];
      let userIds = [];
      for (var i in this.xpList) {
        let r = this.xpList[i];
        if (r.bcpDx == "部门") {
          deptIds.push(r.bcpDeptId)
        }
        if (r.bcpDx == "个人") {
          userIds.push(r.bcpUserId)
        }
      }
      if (deptIds.length > 0) {
        deptsGet(deptIds.join()).then(res => {
          for (var i in res.data.data) {
            this.deptObj[res.data.data[i].deptId] = res.data.data[i];
            for (var j in this.xpList) {
              let r = this.xpList[j];
              if (r.bcpDx == "部门") {
                if (res.data.data[i].deptId == r.bcpDeptId) {
                  this.$set(r, "deptInfo", res.data.data[i]);
                }
              }
            }
          }
        })
      }
      if (userIds.length > 0) {
        usersGet(userIds.join()).then(res => {
          for (var i in res.data.data) {
            this.userObj[res.data.data[i].userId] = res.data.data[i];
            for (var j in this.xpList) {
              let r = this.xpList[j];
              if (r.bcpDx == "个人") {
                if (res.data.data[i].userId == r.bcpUserId) {
                  this.$set(r, "userInfo", res.data.data[i]);
                }
              }
            }
          }

        })
      }
    },
    open(t) {
      this.$emit('openXp', t.id);
    }
  }
}
</script>

<style scoped>
/deep/ .el-table .hzBox {
  border: 0px !important;
  margin: 0;
  padding: 10px 0;
}

.xinYang {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.biaoTiGi{
  width:100%;
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 28px;
  font-weight: bolder;
  /*background: #1f84ec;*/
  background: #b0bde2;
  /*color: #fff;*/
}
</style>
